//
//  XJYLoginTextCell.m
//  LoginModule
//
//  Created by 谢俊逸 on 2016/11/27.
//  Copyright © 2016年 谢俊逸. All rights reserved.
//
#import "Masonry.h"
#import "XJYLoginTextCell.h"
#import "XJYHeader.h"

@implementation XJYLoginTextCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        
#pragma mark Notification 
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(resignLoginTextCellKeyboradFirstResponder) name:@"resignLoginTextCellKeyboradFirstResponder" object:nil];
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        self.userInteractionEnabled = YES;
        if (!_textField) {
            _textField = [UITextField new];
            
            [_textField setFont:[UIFont systemFontOfSize:17]];
            [_textField addTarget:self action:@selector(editDidBegin:) forControlEvents:UIControlEventEditingDidBegin];
            [_textField addTarget:self action:@selector(textValueChanged:) forControlEvents:UIControlEventEditingChanged];
            [_textField addTarget:self action:@selector(editDidEnd:) forControlEvents:UIControlEventEditingDidEnd];
            self.textField.userInteractionEnabled = YES;

            _textField.layer.masksToBounds = YES;
            _textField.layer.cornerRadius = 2.0;
            
            _textField.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.2];
            _textField.font = [UIFont systemFontOfSize:16];
//            _textField.contentScaleFactor = 0.5;
            _textField.adjustsFontSizeToFitWidth = YES;
            _textField.layer.cornerRadius = 5;
            //文字偏移
            _textField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 15, 0)];
            //设置显示模式为永远显示(默认不显示)
            _textField.leftViewMode = UITextFieldViewModeAlways;
            _textField.leftView.userInteractionEnabled = NO;
            _textField.textColor = [UIColor whiteColor];
            [self.contentView addSubview:_textField];
            [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(10, kLoginPaddingLeftWidth, 0, kLoginPaddingLeftWidth));
            }];
        }
    }
    

    
    return self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (void)setPlaceholder:(NSString *)phStr value:(NSString *)valueStr{
    self.textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:phStr? phStr: @"" attributes:@{NSForegroundColorAttributeName: [UIColor colorWithHexString:_isForLoginVC? @"0xffffff": @"0x999999" andAlpha:_isForLoginVC? 0.5: 1.0]}];
    self.textField.text = valueStr;
}

#pragma mark UITextField

#pragma mark TextField
- (void)editDidBegin:(id)sender {
    if (self.editDidBeginBlock) {
        self.editDidBeginBlock(self.textField.text);
    }
}

- (void)editDidEnd:(id)sender {
    if (self.editDidEndBlock) {
        self.editDidEndBlock(self.textField.text);
    }
}

- (void)textValueChanged:(id)sender {
    NSLog(@"文本变化了");
    if (self.textValueChangedBlock) {
        self.textValueChangedBlock(self.textField.text);
    }
}


#pragma mark Notification
#pragma Notification action
- (void)resignLoginTextCellKeyboradFirstResponder{
    [_textField resignFirstResponder];
}

@end
